let mine = document.querySelector(".appointmentbox .mine");
let start = document.querySelector(".appointmentbox .start");
let err = document.querySelectorAll(".err");
let name = document.querySelector(".name");
let tel = document.querySelector(".tel");
let idnum = document.querySelector(".idnum");
let regname = /^[\u4e00-\u9fa5]{2,4}$/;
let regtel = /^[1][3,4,5,7,8,9][0-9]{9}$/;
let regid = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
let f = 0;

// 首页判断input 里面的值是否正确
document.addEventListener("input", function(e) {
	let target = e.target;
	switch (target.className) {
		case "name":
			if (!regname.test(name.value)) {
				err[0].style.opacity = "1";
			} else {
				err[0].style.opacity = "0";
				sessionStorage.setItem("姓名",name.value);
			};
			break;

		case "tel":
			if (!regtel.test(tel.value)) {
				err[1].style.opacity = "1";
			} else {
				err[1].style.opacity = "0";
				sessionStorage.setItem("电话",tel.value);
			};
			break;

		case "idnum":
			if (!regid.test(idnum.value)) {
				err[2].style.opacity = "1";
			} else {
				err[2].style.opacity = "0";
				sessionStorage.setItem("身份证",idnum.value);
			}
			break;
		default:
			break;
	}
	if (regname.test(name.value) && regtel.test(tel.value) && regid.test(idnum.value) ) {
		f = 1;
		console.log("right")
	} else {
		f = 0;
		console.log("wrong")
	}
})

// 数据正确后点击转送到后台，跳转页面
start.addEventListener("click", () => {
	_ajax({
		url: apiUrl.register,
		method: "POST",
		data: {
			name: name.value,
			phone: tel.value,
			card: idnum.value
		}
	}).then((res) => {
		let val = JSON.parse(res);
		console.log(val)
		if (val.msg == "登录成功") {
			sessionStorage.setItem("token", val.result.token);
			window.location.href = "appointment.html";
		}
		console.log(val.result.token)
		// 建立新字段是setItem,获取用getItem
	}).catch(err => {
		console.log("请输入完整的信息");
	})
})

mine.addEventListener("click", () => {
	if (f == 1) {
		window.location.href = "mine.html";
		console.log("right2")
	}
})
 


